//
//  LiveSildingViewController.m
//  project-demo-oc
//
//  Created by 赵隆杰 on 2021/6/15.
//

#import "LiveSildingViewController.h"
#import "LiveSildingViewModel.h"
#import "LiveSildingModel.h"
#import "LiveSildingTableView.h"

/*
 直播上下滑的逻辑
 可上滑，也可下滑，都拉取新数据
 请求的数据全部缓存下来，以便无请求滑回去
 */

@interface LiveSildingViewController ()<LiveSildingTableViewDelegate>

@property (nonatomic, strong) LiveSildingTableView *tableView;

@property (nonatomic, strong) LiveSildingViewModel *viewModel;

@end

@implementation LiveSildingViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    self.viewModel = [[LiveSildingViewModel alloc] init];
    
    self.tableView = [[LiveSildingTableView alloc] init];
    self.tableView.playerTableDelegate = self;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [self first_fetchMoreVideoList];
}

-(void)first_fetchMoreVideoList
{
    [self.viewModel first_fetchMoreVideoList:^{
        //刷新列表
        [self.tableView reloadList:self.viewModel.playerList];
        
        if (self.viewModel.playerList.count > 2) {
            //将传入数据显示列表中间
            [self moveToOneIndex];
        } else {
            [self.tableView first_didScrollView:self.viewModel.playerList.count];
        }
    } withEnterId:@(0)];
    
}

#pragma mark - LiveSildingTableViewDelegate
/// 预请求 下滑
-(void)pre_fetchMoreVideoList:(NSInteger)count
{
    [self.viewModel pre_fetchMoreVideoList:^{
        //刷新列表
        [self.tableView reloadList:self.viewModel.playerList];
        
        //将当前index移到中间1
        [self moveToOneIndex];
    }];
    
}

/// 预请求 上滑
- (void)pre_fetchMoreFrontVideoList:(NSInteger)count
{
    [self.viewModel pre_fetchMoreFrontVideoList:^{
        //刷新列表
        [self.tableView reloadList:self.viewModel.playerList];
        
        //将当前index移到中间1
        [self moveToOneIndex];
    }];
}

- (void)backBtnClick {
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)moveToOneIndex
{
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
}


@end
